package com.jyserver.common.AutoCode.jybaits.element.pc.Tables;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jyserver.common.utils.HelpUtils;

import java.util.ArrayList;
import java.util.List;

public class table {
    private List<String> columns = new ArrayList<String>();
    private List<String> imports = new ArrayList<String>();
    private List<String> components = new ArrayList<String>();

    private List<String> methods = new ArrayList<String>();
    private List<String> mounteds = new ArrayList<String>();

    private List<String> computeds = new ArrayList<String>();

    private List<String> filterss = new ArrayList<String>();

    private List<String> datas = new ArrayList<String>();

    private List<String> beforeCreates = new ArrayList<String>();
    private String postapi = null;

    private String header = "<el-table\n" +
            "    :data=\"tableData\"\n" +
            "    tooltip-effect=\"dark\"\n" +
            "    v-loading=\"loadingShow\"\n" +
            "    element-loading-text=\"拼命加载中\"\n" +
            "    element-loading-spinner=\"el-icon-loading\"\n" +
            "    element-loading-background=\"rgba(0, 0, 0, 0.8)\"\n" +
            "    style=\"width: 100%\"\n" +
            "   >\n";
    private String endString = "</el-table>\n";



    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }

    public List<String> getImports() {
        return imports;
    }

    public void setImports(List<String> imports) {
        this.imports = imports;
    }

    public List<String> getComponents() {
        return components;
    }

    public void setComponents(List<String> components) {
        this.components = components;
    }

    public List<String> getMethods() {
        return methods;
    }

    public void setMethods(List<String> methods) {
        this.methods = methods;
    }

    public List<String> getMounteds() {
        return mounteds;
    }

    public void setMounteds(List<String> mounteds) {
        this.mounteds = mounteds;
    }

    public List<String> getBeforeCreates() {
        return beforeCreates;
    }

    public void setBeforeCreates(List<String> beforeCreates) {
        this.beforeCreates = beforeCreates;
    }

    public String getPostapi() {
        return postapi;
    }

    public void setPostapi(String postapi) {
        this.postapi = postapi;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public String getEndString() {
        return endString;
    }

    public void setEndString(String endString) {
        this.endString = endString;
    }

    public table() {
        basemethod();
    }


    public void basemethod() {
        methods.add("      handleEdit(index, row) {\n" +
                "        console.log(index, row);\n" +
                "      },\n");
        methods.add("      handleDelete(index, row) {\n" +
                "        console.log(index, row);\n" +
                "      },\n");
        methods.add("      handleView(index, row) {\n" +
                "        console.log(index, row);\n" +
                "      },\n");
        methods.add("formatterOperation(row, column, cellValue, index) {\n" +
                "        return row.a = false\n" +
                "      },");
    }
    public  table(Object value,String createAPI, String handleViewAPI, String handleDeleteAPI,String updateApi, String postapi) {
        this.postapi=postapi;
        basemethod(createAPI, handleViewAPI, handleDeleteAPI,updateApi);
        addcolumnAll(value);
        AddoperationString();
        addgetTableMethods();
    }

    public void basemethod(String createAPI, String handleViewAPI, String handleDeleteAPI,String updateApi) {
        methods.add("handleEdit(index, row) {\n" +
                "        this.axios.post('" + handleViewAPI + "', this.$qs.stringify({id: row.id}))\n" +
                "          .then((response) => {\n" +
                "            if (response.data.success) {\n" +
                "              this.Dataform = response.data.data;\n" +
                "              this.dialog.edit = true\n" +
                "              this.dialog.title = \"修改模块信息\"\n" +
                "              this.formurl = '"+updateApi+"'\n" +
                "            }\n" +
                "          })\n" +
                "          .catch((error) => {\n" +
                "          })\n" +
                "      },\n");
        methods.add("handleDelete(index, row) {\n" +
                "        this.axios.post('"+handleDeleteAPI+"', this.$qs.stringify({id: row.id}))\n" +
                "          .then((response) => {\n" +
                "            if (response.data.success) {\n" +
                "              this.$message({\n" +
                "                message: '删除成功',\n" +
                "                type: 'success'\n" +
                "              });\n" +
                "              this.getTable()\n" +
                "            }\n" +
                "          })\n" +
                "          .catch((error) => {\n" +
                "          })\n" +
                "      },\n");

        methods.add("handleView(index, row) {\n" +
                "        this.axios.post('"+handleViewAPI+"', this.$qs.stringify({id: row.id}))\n" +
                "          .then((response) => {\n" +
                "            if (response.data.success) {\n" +
                "              this.Viewform = response.data.data\n" +
                "              this.dialog.view = true\n" +
                "              this.dialog.title = \"图书详情\"\n" +
                "            }\n" +
                "          })\n" +
                "          .catch((error) => {\n" +
                "          })\n" +
                "      },\n");
        methods.add("formatterOperation(row, column, cellValue, index) {\n" +
                "        return row.a = false\n" +
                "      },\n");
        methods.add("submitForm(form) {\n" +
                "        this.$refs[form].validate((valid) => {\n" +
                "          if (valid) {\n" +
                "            this.axios.post(this.formurl, this.Dataform)\n" +
                "              .then((response) => {\n" +
                "                if (response.data.success) {\n" +
                "                  this.dialog.edit = false\n" +
                "                  this.getTable();\n" +
                "                  this.$message({\n" +
                "                    message: '请求成功',\n" +
                "                    type: 'success'\n" +
                "                  });\n" +
                "                }\n" +
                "              });\n" +
                "          } else {\n" +
                "            console.log('error submit!!');\n" +
                "            return false;\n" +
                "          }\n" +
                "        });\n" +
                "      },\n");
        methods.add("resetForm(formName) {\n" +
                "        if (this.Dataform.id) {\n" +
                "          this.handleEdit(null, {id: this.Dataform.id})\n" +
                "        } else {\n" +
                "          this.$refs[formName].resetFields();\n" +
                "        }\n" +
                "      },\n" );
        methods.add("createRecord() {\n" +
                "        this.dialog.edit = true\n" +
                "        this.dialog.title = \"创建模块\"\n" +
                "        this.Dataform=this.DataformTest\n" +
                "        this.formurl = '"+createAPI+"'\n" +
                "      },\n");
        mounteds.add(" this.getTable()\n");
        datas.add("formLabelWidth: \"150px\",\n");
        datas.add("formLabelWidthView: \"150px\",\n");
        datas.add("tableData: [],\n");
        datas.add("loadingShow: true,\n");
        datas.add("params: {userId: \"\",search:\"\"},\n");
        datas.add("dialog: {title: \"模块详情\", view: false, edit: false},\n");
        datas.add("formurl:\""+createAPI+"\",\n");
    }

    public table(Boolean selection) {
        if (selection) {
            columns.add("<el-table-column\n" +
                    "            type=\"selection\"\n" +
                    "    width=\"55\">\n" +
                    "    </el-table-column>\n");
            header = "<el-table\n" +
                    "    ref=\"multipleTable\"\n" +
                    "    :data=\"tableData\"\n" +
                    "    tooltip-effect=\"dark\"\n" +
                    "    v-loading=\"loadingShow\"\n" +
                    "    element-loading-text=\"拼命加载中\"\n" +
                    "    element-loading-spinner=\"el-icon-loading\"\n" +
                    "    element-loading-background=\"rgba(0, 0, 0, 0.8)\"\n" +
                    "    style=\"width: 100%\"\n" +
                    "    @selection-change=\"handleSelectionChange\">\n";
            methods.add("toggleSelection(rows) {\n" +
                    "        if (rows) {\n" +
                    "          rows.forEach(row => {\n" +
                    "            this.$refs.multipleTable.toggleRowSelection(row);\n" +
                    "          });\n" +
                    "        } else {\n" +
                    "          this.$refs.multipleTable.clearSelection();\n" +
                    "        }\n" +
                    "      },\n");
            methods.add("handleSelectionChange(val) {\n" +
                    "        this.multipleSelection = val;\n" +
                    "      },\n");
        }
        basemethod();
    }


    public void addgetTableMethods() {
        methods.add("getTable(){\n" +
                "        this.axios.post('"+postapi+"', this.params)\n" +
                "          .then((response) => {\n" +
                "            if (response.data.success) {\n" +
                "              this.tableData = response.data.data\n" +
                "              this.loadingShow=false\n" +
                "            }else {\n" +
                "              this.loadingShow=false\n" +
                "            }\n" +
                "          })\n" +
                "          .catch((error) => {\n" +
                "            this.$message({\n" +
                "              message:\"加载失败\",\n" +
                "              type: 'error'\n" +
                "            });\n" +
                "          })\n" +
                "      },");
    }

    public void addsortCchangeMethods() {
        String str = "sortCchange(coloumn,prop,order){console.log(column)},\n";
        if (!methods.contains(str)) {
            mounteds.add(str);
        }
    }


    public void addcolumns(String label, String prop, Object sortable) {
        String str = "";
        if (HelpUtils.isNotEmpty(sortable)) {
            str = "    sortable=\"custom\"\n";
            if (header.indexOf("selection") > 0) {
                header = "<el-table\n" +
                        "    ref=\"multipleTable\"\n" +
                        "    :data=\"tableData\"\n" +
                        "    tooltip-effect=\"dark\"\n" +
                        "    v-loading=\"loadingShow\"\n" +
                        "    element-loading-text=\"拼命加载中\"\n" +
                        "    element-loading-spinner=\"el-icon-loading\"\n" +
                        "    element-loading-background=\"rgba(0, 0, 0, 0.8)\"\n" +
                        "    style=\"width: 100%\"\n" +
                        "    @sort-change=\"sortCchange\"\n" +
                        "    @selection-change=\"handleSelectionChange\">\n";
            } else {
                header = "<el-table\n" +
                        "    ref=\"multipleTable\"\n" +
                        "    :data=\"tableData\"\n" +
                        "    v-loading=\"loadingShow\"\n" +
                        "    element-loading-text=\"拼命加载中\"\n" +
                        "    element-loading-spinner=\"el-icon-loading\"\n" +
                        "    element-loading-background=\"rgba(0, 0, 0, 0.8)\"\n" +
                        "    tooltip-effect=\"dark\"\n" +
                        "    style=\"width: 100%\"\n" +
                        "    @sort-change=\"sortCchange\">\n";
            }
            addsortCchangeMethods();
        }
        this.columns.add("<el-table-column\n" +
                "      prop=\"" + prop + "\"\n" +
                "      label=\"" + label + "\"\n" + str +
                "      width=\"180\">\n" +
                "    </el-table-column>\n");
    }


    public void addcolumns(String label, String prop) {
        this.columns.add("<el-table-column\n" +
                "      prop=\"" + prop + "\"\n" +
                "      label=\"" + label + "\"\n" +
                "      width=\"180\">\n" +
                "    </el-table-column>\n");
    }

    public void addcolumns(String label, String prop, String sortable, Boolean filters) {
        String str = "";
        if (HelpUtils.isNotEmpty(sortable)) {
            str = "      sortable\n";
        }
        this.columns.add("<el-table-column\n" +
                "      prop=\"" + prop + "\"\n" +
                "      label=\"" + label + "\"\n" + str +
                "      width=\"180\">\n" +
                "    </el-table-column>\n");
    }

    public void addbeforeCreates(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            methods.add(str);
        }
    }

    public String getbeforeCreatesString() {
        String str = "beforeCreate(){\n";
        for (int i = 0; i < beforeCreates.size(); i++) {
            str = str + beforeCreates.get(i);
        }
        return str + "},\n";
    }

    public void addcomputeds(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            computeds.add(str);
        }
    }

    public String getcomputedsString() {
        if (computeds.size()>0){
            String str = "computed{\n";
            for (int i = 0; i < computeds.size(); i++) {
                str = str + computeds.get(i);
            }
            return str + "},\n";
        }
        return "";
    }

    public void addfilterss(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            filterss.add(str);
        }
    }

    public String getfilterssString() {
        if (filterss.size()>0){
            String str = "filters{\n";
            for (int i = 0; i < filterss.size(); i++) {
                str = str + filterss.get(i);
            }
            return str + "},\n";
        }
        return "";
    }

    public void addmethods(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            methods.add(str);
        }
    }

    public String getmethodsString() {
        String str = "methods: {\n";
        for (int i = 0; i < methods.size(); i++) {
            str = str + methods.get(i);
        }
        return str + "},\n";
    }

    public void addmounteds(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            methods.add(str);
        }
    }

    public String getmountedsString() {
        String str = "mounted() {\n";
        for (int i = 0; i < mounteds.size(); i++) {
            str = str + mounteds.get(i);
        }
        return str + "},\n";
    }


    public void addcomponents(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            components.add(str);
        }
    }

    public String getcomponentsString() {
        if (components.size() == 0) {
            return "";
        }
        String str = "components: {\n";
        for (int i = 0; i < components.size(); i++) {
            str = str + components.get(i) + ",\n";
        }
        return str + "},\n";
    }

    public String getcolumnsString() {
        String str = "";
        for (int i = 0; i < columns.size(); i++) {
            str = str + columns.get(i);
        }
        return str;
    }

    public void AddoperationString() {
        String str = "<el-table-column label=\"操作\"  width=\"250\" :formatter=\"formatterOperation\">\n" +
                "        <template slot-scope=\"scope\">\n" +
                "          <el-button\n" +
                "            size=\"mini\"\n" +
                "            @click=\"handleEdit(scope.$index, scope.row)\">编辑\n" +
                "          </el-button>\n" +
                "\n" +
                "          <el-button\n" +
                "            size=\"mini\"\n" +
                "            type=\"success\"\n" +
                "            @click=\"handleView(scope.$index, scope.row)\">查看\n" +
                "          </el-button>\n" +
                "\n" +
                "          <el-popover scope.row.a\n" +
                "                      placement=\"top\"\n" +
                "                      width=\"160\"\n" +
                "                      v-model=\"scope.row.a\"\n" +
                "                      trigger=\"click\">\n" +
                "            <p>确定删除？</p>\n" +
                "            <div style=\"text-align: right; margin: 0\">\n" +
                "              <el-button size=\"mini\" type=\"text\" @click=\"scope.row.a = false\">取消</el-button>\n" +
                "              <el-button type=\"primary\"  @click=\"scope.row.a = false;handleDelete(scope.$index, scope.row)\" size=\"mini\">确定</el-button>\n" +
                "            </div>\n" +
                "            <el-button\n" +
                "              size=\"mini\"\n" +
                "              type=\"danger\"\n" +
                "              slot=\"reference\"\n" +
                "            >删除\n" +
                "            </el-button>\n" +
                "\n" +
                "          </el-popover>\n" +
                "\n" +
                "        </template>\n" +
                "      </el-table-column>";
        this.columns.add(str);
    }

    public void addimport(String import_son) {
        if (HelpUtils.isNotEmpty(import_son)) {
            this.imports.add(import_son);
        }
    }

    public String getimportString() {
        String str = "";
        for (int i = 0; i < imports.size(); i++) {
            str = str + imports.get(i) + "\n";
        }
        return str;
    }

    public void adddatas(String str) {
        if (HelpUtils.isNotEmpty(str)&& !datas.contains(str)){
            datas.add(str);
        }
    }


    public String getdataString() {
        String str = "\ndata() {\nreturn {\n";
        for (int i = 0; i < datas.size(); i++) {
            str = str + datas.get(i);
        }
        return str + "}},\n";
    }

    public String getdataString(String s)  {
        String str = "\ndata() {\n"+s+"return {\n";
        for (int i = 0; i < datas.size(); i++) {
            str = str + datas.get(i);
        }
        return str + "}},\n";
    }


    public String getscript() {
        String str = "\n<script>\n" + getimportString() + "export default {\n" + getcomponentsString() + getfilterssString()+getcomputedsString()+ getdataString() + getbeforeCreatesString() + getmountedsString() + getmethodsString();
        return str + "\n}\n</script>\n" + "<style scoped>\n</style>";
    }

    public String getscript(String s) {
        String str = "\n<script>\n" + getimportString() + "export default {\n" + getcomponentsString() + getfilterssString()+getcomputedsString()+ getdataString(s) + getbeforeCreatesString() + getmountedsString() + getmethodsString();
        return str + "\n}\n</script>\n" + "<style scoped>\n</style>";
    }

    public String creatAll() {
        return header + getcolumnsString() + endString + getscript();
    }

    public String creatAll(String s) {
        return header + getcolumnsString() + endString + getscript(s);
    }

    public String test(Object value, String key1, String key2, Object api) {
        JSONArray array = JSONObject.parseArray(String.valueOf(value));
        postapi = String.valueOf(api).replace("http://d.huayuboneng.com", "");
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObjectSon = JSONObject.parseObject(String.valueOf(array.get(i)));
            String note = String.valueOf(jsonObjectSon.get("note"));
            String key = String.valueOf(jsonObjectSon.get("key"));
            addcolumns(note, key, jsonObjectSon.get("sortable"));
        }
        header = "<!--" + key1 + "_" + key2 + "-->\n" + header;
        addgetTableMethods();
        AddoperationString();
        return creatAll();
    }

    public void addcolumnAll(Object value){
        JSONArray array = JSONObject.parseArray(String.valueOf(value));
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObjectSon = JSONObject.parseObject(String.valueOf(array.get(i)));
            String note = String.valueOf(jsonObjectSon.get("note"));
            String key = String.valueOf(jsonObjectSon.get("key"));
            addcolumns(note, key, jsonObjectSon.get("sortable"));
        }
    }
}
